<?
//ob
ob_start();

//configuration
include "../conf.php";

//includes
include "../php/functions.php";

//connect2db
$_DB = new PDO("mysql:host={$_MYSQL['host']};dbname={$_MYSQL['db_name']}", $_MYSQL['db_user'], $_MYSQL['password']);

if($_GET['do']=='add' AND $_GET['file'] AND $_GET['title'] AND $_GET['length']) {
	$_DB->prepare('INSERT INTO `songs` (`mp3name`,   `title`, `length`,  `hash`) VALUES( :mp3name, :title ,:length, :hash)')->execute(array(
		'mp3name'=>$_GET['file'],
		'title'=>$_GET['title'],
		'length'=>$_GET['length'],
		'hash'=>song_hash($_GET['file'],$_GET['title'],$_GET['length'])));
	header("Location: {$_SERVER['PHP_SELF']}");
}
if($_GET['do']=='update' AND $_GET['file'] AND $_GET['title'] AND $_GET['length']  AND $_GET['hash']) {
	$_DB->prepare('UPDATE `songs` SET `mp3name` = :mp3name , `title` = :title , `length` = :length , `hash` = :newhash WHERE `hash` = :oldhash')->execute(array(
		'mp3name'=>$_GET['file'],
		'title'=>$_GET['title'],
		'length'=>$_GET['length'],
		'newhash'=>song_hash($_GET['file'],$_GET['title'],$_GET['length']),
		'oldhash'=>$_GET['hash']));
	header("Location: {$_SERVER['PHP_SELF']}");
}
if($_GET['do']=='delete' AND $_GET['hash']) {
	$_DB->prepare('DELETE FROM `songs` WHERE `hash`= :hash LIMIT 1;')->execute(array(
		'hash'=> $_GET['hash']));
	header("Location: {$_SERVER['PHP_SELF']}");
}

?>
<h2>Songs</h2>
<?
if($_GET['do']=='edit' AND $_GET['hash']) {
	$song=$_DB->prepare('SELECT *  FROM `songs` WHERE `hash`= :hash LIMIT 1;');
	$song->execute(array(	'hash'=> $_GET['hash']));$song=$song->fetch(); 
	?>
	<h3>Edit Song:</h3>
	<form>
		File: <input name=file value="<?=$song['mp3name']?>"><br>
		Title: <input name=title value="<?=$song['title']?>"><br>
		Length: <input name=length value="<?=$song['length']?>"><br>
		<input type=hidden name=hash value="<?=$song['hash']?>">
		<input type=submit name=do value=update>
	</form>
	<?
}
?>
<table>
<tr><td>File</td><td>Titile</td><td>length</td><td>Hash</td><td></td></tr>
<?
foreach($_DB->query('SELECT * FROM `songs`')->fetchAll() as $song) {
	?><tr><td><?=$song['mp3name']?></td><td><?=$song['title']?></td><td><?=$song['length']?></td><td><?=$song['hash']?></td><td><form><input type=hidden name=hash value="<?=$song['hash']?>"><input type=submit name=do value=delete><input type=submit name=do value=edit></form></td></tr><?
}
?>
</table>
<br><br>
<h3>Add Song:</h3>
<form>
	File: <input name=file><br>
	Title: <input name=title><br>
	Length: <input name=length><br>
	<input type=hidden name=do value=add>
	<input type=submit>
</form>